package com.yipao.wx.web.operate;

import com.yipao.core.util.ResponseUtil;
import com.yipao.core.validator.Order;
import com.yipao.core.validator.Sort;
import com.yipao.db.domain.devinfo.DevInfo;
import com.yipao.db.domain.goods.Category;
import com.yipao.db.domain.goods.Goods;
import com.yipao.db.domain.goods.DevGoods;
import com.yipao.db.domain.goods.vo.DevGoodsBatchInsertReq;
import com.yipao.db.domain.goods.vo.DevGoodsBindDVo;
import com.yipao.db.domain.goods.vo.DevGoodsBindVo;
import com.yipao.db.service.CategoryService;
import com.yipao.db.service.devinfo.DevGoodsService;
import com.yipao.db.service.devinfo.DevInfoService;
import com.yipao.db.service.goods.GoodsService;
import com.yipao.wx.vo.DevGoodsVo;
import com.yipao.wx.vo.DevGoodsVoList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备产品
 *
 * @author tianhao
 */
@RestController
@RequestMapping("/wx/dev/goods")
@Validated
@Slf4j
public class WxDevGoodsController {
    @Resource
    private DevGoodsService devGoodsService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private CategoryService categoryService;



    @PostMapping("/bindings")
    public Object bindings(@RequestBody DevGoodsVoList devGoodsVoList) {
        List<DevGoodsVo> goodsVoList = devGoodsVoList.getGoodsVoList();
        for (DevGoodsVo devGoodsVo : goodsVoList) {
            // 先将设备下的产品全部删除
            devGoodsService.updateDevGoodsDeleteStatus(devGoodsVo.getDevId());
            // 批量绑定商品
            devGoodsService.insertBatch(devGoodsVo.getDevId(),devGoodsVo.getGoodsId());
        }
        return ResponseUtil.ok();
    }


    /**
     * 商品和设备绑定
     *
     * @param devGoods
     * @return int
     */
    @PostMapping("/insert")
    public Object insert(@RequestBody DevGoods devGoods) {
        return ResponseUtil.ok(devGoodsService.insert(devGoods));
    }


    /**
     * 批量新增
     *
     * @return
     */
    @PostMapping("/insert/batch")
    public Object insertBatch(@RequestBody DevGoodsBatchInsertReq req) {
        return ResponseUtil.ok(devGoodsService.insertBatch(req.getDevId(), req.getGoodsIds()));
    }

    /**
     * 删除设备产品
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestParam @NotNull Integer id) {
        return ResponseUtil.ok(devGoodsService.updateDevGoodsDeleteStatus(id));
    }

    /**
     * 修改
     *
     * @param devGoods
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DevGoods devGoods) {
        return ResponseUtil.ok(devGoodsService.update(devGoods));
    }


    /**
     * 查询指定设备绑定的商品列表
     *
     * @param devId 设备id
     * @return
     */
    @GetMapping("/list")
    public Object queryDevGoodsByDevId(@RequestParam @NotNull String devId) {
        return ResponseUtil.ok(goodsService.queryDevGoodsByDevId(devId));
    }

    /**
     * 查询分类下的所有产品
     *
     * @param
     * @return
     */
    @GetMapping("/findList")
    public Object findList(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit) {
        DevGoodsBindVo dv = null;
        List<DevGoodsBindVo> goodsBindVoList = new ArrayList<>();


        List<Category> all = categoryService.findAll(page, limit);
        for (Category category : all) {
            List<DevGoodsBindDVo> children = new ArrayList<>();
            dv = new DevGoodsBindVo();
            dv.setId(category.getId());
            dv.setText(category.getName());
            dv.setValue(category.getId() + "");
            goodsBindVoList.add(dv);


            List<Goods> yipaoGoods = goodsService.queryByCategory(dv.getId(), page, limit);
            if (yipaoGoods!=null && yipaoGoods.size()>0){
            for (Goods yipaoGood : yipaoGoods) {
                DevGoodsBindDVo dbv = new DevGoodsBindDVo();
                dbv.setId(yipaoGood.getId());
                dbv.setText(yipaoGood.getName());
                dbv.setValue(yipaoGood.getId() + "");
                children.add(dbv);
            }
                dv.setChildren(children);
            }
        }
        return ResponseUtil.ok(goodsBindVoList);

    }








}
